HTML - <button> Tag



The HTML <button> element is an interactive element that is used to create a button in HTML. It is activated by a user with the mouse, keyboard, finger, voice, command, etc.

Once the button is activated, then it performs different actions submitting form data, deleting data, opening a pop-up dialog box, etc. By default, HTML buttons are rendered in a style that matches the platform the user is running on, but you can change button styles such as color, width, height, etc, using CSS.

Syntax

Following is the syntax for HTML <button> tag −

<button>button name</button>

Specific Attributes

The HTML <button> tag also supports the following additional attributes −

S.No. Attribute & Description
1

autofocus

Specifies that the button should have input focus when the page loads.
2

disabled

Specifies the button is disabled.
3

form

Specifies the forms to which button belongs.
4

formaction

Specifies the link where the form submits.
5

formenctype

Specifies how the form data is encoded before sending it to server.
6

formmethod

Specifies how to send form data.
7

formnovalidate

Specifies that the form data should not be validated.
8

formtarget

Specifies where the response should be validated.
9

name

Specifies the button name.
10

type

Specifies the button type.
11

value

Specifies button's initial value.

Example

In the following program, we are using the HTML <button> tag to create a button with the name "click" in HTML.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML Button</title>
</head>
<body>
   <!--create a button-->
   <h1>HTML button</h1>
   <button>click</button>
</body>
</html>

When we run the above code, it will generate an output consisting of the text along with a click button on the webpage.

Example

Following is another example of an HTML <button> tag. Here, we are creating an HTML button named "ClickMe!" Using the <button> tag, and, we are using CSS to style the button we have created.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML Button</title>
   <style>
      button {
         width: 100px;
         height: 30px;
         background-color: green;
         color: white;
         cursor: pointer;
      }
   </style>
</head>
<body>
   <!--create a button-->
   <h1>HTML button</h1>
   <button>ClickMe!</button>
</body>
</html>

On running the above code, the output window will pop up displaying the click button along with a CSS applied to it on the webpage.

Example

In this example, we are creating multiple buttons in HTML using the <button> tag. Then, we are using CSS to give different styles to each created button.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML Button</title>
   <style>
      button:nth-child(2) {
         background-color: aqua;
         width: 100px;
         height: 30px;
         border-radius: 10%;
      }

      button:nth-child(3) {
         background-color: green;
         width: 130px;
         height: 30px;
         color: white;
      }

      button:nth-child(4) {
         background-color: blueviolet;
         width: 90px;
         height: 30px;
         color: white;
         border-radius: 10px;
      }

      button:nth-child(5) {
         background-color: red;
         width: 80px;
         height: 30px;
         cursor: pointer;
         border-radius: 5px;
      }

      button:nth-child(3):hover {
         background-color: transparent;
         color: black;
         cursor: pointer;
      }
   </style>
</head>
<body>
   <!--create a button-->
   <h1>HTML buttons</h1>
   <button>Click</button>
   <button>Hover me!</button>
   <button>Submit</button>
   <button>Danger</button>
   <button>Default</button>
</body>
</html>

When we run the above code, it will generate an output consisting of the buttons applied with different CSS properties applied to it on the webpage.

Example

In this program, we are creating an HTML button using the <button> tag. Then, we are using the "disabled" attribute to disable the created button..

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML Button</title>
</head>
<body>
   <!--create a button-->
   <h1>HTML button</h1>
   <button disabled>Submit</button>
</body>
</html>

On running the above code, the output window will pop up displaying the text along with click button that is disabled on the webpage.

html_tags_reference.htm
Advertisements